
/**
 * Created with Intellij JDEA
 * Description:
 * User:
 * Date:2022-03-14
 * Time:19:19
 */

public class ListNode {
    public int val;
    public ListNode next;

    public ListNode(int val) {
        this.val = val;
    }

    //反转一个单链表
    public ListNode reverseList(ListNode head) {
        if (head == null) {
            return null;
        }
        ListNode prev = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return prev;
    }


    //给定一个带有头结点 head 的非空单链表，返回链表的中间结点。如果有两个中间结点，则返回第二个中间结点
    public ListNode middleNode(ListNode head) {
        if (head == null) {
            return null;
        }
        if (head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    /**
     * 找到单链表 倒数第K个节点
     * 要求 只遍历链表一遍
     * @param k
     * @return
     */
    public ListNode findKthToTail(ListNode head,int k) {
        //k是否是合法的
        if(k <= 0 || head == null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k; i++) {
            if (fast ==null) {
                return null;
            }
            fast = fast.next;
        }
        while (fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


    //将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的
    public static ListNode mergeTwoLists(ListNode head1, ListNode head2) {
        ListNode newNode = new ListNode(-1);
        ListNode tmp = newNode;
        while (head1 != null && head2 != null) {
            if (head1.val < head2.val) {
                tmp.next = head1;
                tmp = tmp.next;
                head1 = head1.next;
            }else {
                tmp.next = head2;
                tmp = tmp.next;
                head2 = head2.next;
            }
        }
        if(head1 == null && head2 != null) {
            tmp.next = head2;
        } else {
            tmp.next = head1;
        }
        return newNode.next;
    }


    //编写代码，以给定值x为基准将链表分割成两部分，所有小于x的结点排在大于或等于x的结点之前
    public  ListNode partition(ListNode pHead, int x) {
        // write code here
        if (pHead == null) {
            return null;
        }
        ListNode bs = null;
        ListNode be = null;
        ListNode as = null;
        ListNode ae = null;

        ListNode cur = pHead;
        while (cur != null) {
            if (cur.val < x) {
                if (bs == null) {
                    bs = cur;
                    be = cur;
                } else {
                    be.next = cur;
                    be = be.next;
                }
            } else {
                if (as == null) {
                    as = cur;
                    ae = cur;
                } else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }

        if (bs == null) {
            return as;
        }
        be.next = as;
        if (as != null) {
            ae.next = null;
        }
        return bs;
    }

    /**
     * 判断链表是不是回文链表
     * @return
     */
    public ListNode A;

    public boolean chkPalindrome() {
        //1、找到链表的中间节点

        if (this.A == null) {
            return true;
        }
        if (this.A.next == null) {
            return true;
        }
        ListNode fast = this.A;
        ListNode slow = this.A;
        while (fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //2、翻转

        ListNode cur = slow.next;

        while (cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //3、开始一个从头走，一个从尾巴走，直到相遇

        while (A != slow) {
            if (this.A.val != slow.val) {
                return false;
            }
            if (A.next == slow){
                return true;
            }
                A = A.next;
                slow = slow.next;

        }
        return true;
    }

    /**
     * 求两个链表的相交的节点
     * @param headA
     * @param headB
     * @return
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if(headA == null || headB == null) {
            return null;
        }
        ListNode pl = headA;//指向长的链表
        ListNode ps = headB;//指向短的链表
        //1、分别求长度
        int lenA = 0;
        int lenB = 0;
        while (pl != null) {
            lenA++;
            pl = pl.next;
        }

        while (ps != null) {
            lenB++;
            ps = ps.next;
        }
        //需要重新修改指向
        pl = headA;
        ps = headB;

        //2、已经知道两个链表的长度了
        int len = lenA-lenB;
        if(len < 0) {
            pl = headB;
            ps = headA;
            len = lenB - lenA;
        }
        //1、求长度    2、pl永远指向 最长的那个链表   ps永远指向最短的那个链表

        //3、就是让最长的链表走len步
        while (len != 0){
            pl = pl.next;
            len--;
        }
        //4、一人一步走 直到相遇
        while (pl != ps) {
            pl = pl.next;
            ps = ps.next;
        }
        //5、是不是两个都是null-》没有相交
        /*if (pl == null){
            return null;
        }*/
        return pl;
    }
    public ListNode head;

    /**
     * 给定一个链表，判断链表中是否有环。
     * 判断链表是否有环
     * @param head
     * @return
     */
    public boolean hasCycle(ListNode head) {

        if (head == null) {
            return false;
        }

        if (head.next == null) {
            return false;
        }
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }

    /**
     * 求环的入口点
     * @param head
     * @return
     */
    public ListNode detectCycle(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        if (head == null) {
            return null;
        }

        if (head.next == null) {
            return null;
        }
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;

    }

    //链表删除所有元素
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return head;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        if (head.val == val) {
            head = head.next;
        }
        return head;
    }



}
